/**
 * Created by Administrator on 2016/8/30 0030.
 */
(function (){
    var app = angular.module('s9.service',[]);
    app.service('modelService', function () {
        // 数据存放的地方
        var data = [
            {name: '张三', phone: '18612345673', state: '邀请中'},
            {name: '李四', phone: '18612345674', state: '已接受'},
            {name: '王五', phone: '18612345675', state: '邀请中'},
            {name: '赵六', phone: '18612345676', state: '已拒绝'},
        ];
        // 嘉宾的构造函数
        function Guest(name, phone) {
            this.name = name;
            this.phone = phone;
            this.state = '邀请中'
        }

        // 下面的四个函数是我们对数据的一些操作

        // 用于往数据源里添加新的一条数据
        this.add = function (name, phone, callback) {
            data.push(new Guest(name, phone));
            var isConnectSuccess = true; // 网络访问的成功与否
            if (isConnectSuccess) {
                callback({status: "success", message: '新增成功'})
            } else {
                callback({status: "failed", message: '新增失败'})
            }
        };
        // 从数据源中删除项目
        this.del = function (guest, callback) {
            // 过滤一下：除了电话和传过来的guest的电话相同的嘉宾，其他的都留下
            data = data.filter(function (item) {
                return item.phone != guest.phone;
            });
            callback({status: "success", message: '删除成功'})
        };
        // 根据条件从数据源中获取一个列表
        this.getList = function (state, callback) {
            if (state == '全部') {
                var result = data.filter(function () {
                    return true;
                });
                callback({status: 'success', data: result})
            } else {
                var result_filtered = data.filter(function (item) {
                    return item.state == state;
                });
                callback({status: 'success', data: result_filtered});
            }
        };
        // 修改用户状态
        this.setState = function (guest, state, callback) {
            guest.state = state;
            callback({status: "success", message: '设置状态成功'})
        }
    });



})()